package com.offcn.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.offcn.mapper.CheckgroupCheckitemMapper;
import com.offcn.mapper.CheckitemMapper;
import com.offcn.pojo.CheckgroupCheckitem;
import com.offcn.pojo.Checkitem;
import com.offcn.service.CheckitemService;
import com.offcn.utils.PageResult;
import com.offcn.utils.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: wy
 * @Date: 2022/8/29 15:46
 */
@Component
@Service
public class CheckitemServiceImpl implements CheckitemService {

    @Autowired
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;
    @Autowired
    private CheckitemMapper checkitemMapper;

    @Override
    public void add(Checkitem checkitem) {
        checkitemMapper.insert(checkitem);
    }

    //进行分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<Checkitem> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        QueryWrapper<Checkitem> queryWrapper = new QueryWrapper<>();

        Page<Checkitem> checkitemPage = null;
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString() != "") {

            queryWrapper.like("code", queryPageBean.getQueryString());
            queryWrapper.or();
            queryWrapper.like("name", queryPageBean.getQueryString());
            checkitemPage = checkitemMapper.selectPage(page, queryWrapper);
        } else {
            checkitemPage = checkitemMapper.selectPage(page, queryWrapper);
        }

        //吧当前页的数据 存放 PageResult 对象中
        PageResult pageResult = new PageResult(checkitemPage.getTotal(), checkitemPage.getRecords());
        return pageResult;
    }

    @Override
    public Checkitem updatelist(Checkitem checkitem) {
        Checkitem checkitem1 = checkitemMapper.selectById(checkitem.getId());
        return checkitem1;
    }

    @Override
    public void updateInfoById(Checkitem checkitem) {
//        LambdaQueryWrapper<Checkitem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//
//        Checkitem checkitem1 = new Checkitem();
//        checkitem1.setAge(checkitem.getAge());
//        checkitem1.setAttention(checkitem.getAttention());
//        checkitem1.setCode(checkitem.getCode());
//        checkitem1.setName(checkitem.getName());
//        checkitem1.setPrice(checkitem.getPrice());
//        checkitem1.setSex(checkitem.getSex());
//        checkitem1.setType(checkitem.getType());
//        checkitem1.setRemark(checkitem.getRemark());
//        lambdaQueryWrapper.eq(Checkitem::getId, checkitem.getId());
//        checkitemMapper.update(checkitem1, lambdaQueryWrapper);

        //根据ID进行修改
        checkitemMapper.updateById(checkitem);
    }

    //通过id进行删除
    @Override
    @Transactional
    public void deleteInfoByid(Integer id) {
        //先 删除中中间表 -- 检查组检查项
        LambdaQueryWrapper<CheckgroupCheckitem> checkitemLambdaQueryWrapper =
                new LambdaQueryWrapper<CheckgroupCheckitem>();
        checkitemLambdaQueryWrapper.eq(CheckgroupCheckitem::getCheckitemId, id);
        checkgroupCheckitemMapper.delete(checkitemLambdaQueryWrapper);

        //删除checkitem 表
        checkitemMapper.deleteById(id);
    }

    //查询检查项的方法
    @Override
    public List<Checkitem> showAllItem() {
        return checkitemMapper.selectList(null);
    }

    //根据id进行查询 到检查组下的 检查 事项
    @Override
    public List<Integer> getCheckItemIdByGroupId(Integer id) {
        LambdaQueryWrapper<CheckgroupCheckitem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckgroupCheckitem::getCheckgroupId, id);
        List<CheckgroupCheckitem> checkgroupCheckitems = checkgroupCheckitemMapper.selectList(lambdaQueryWrapper);

        List<Integer> checkitemId =
                checkgroupCheckitems.stream()
                        .map(checkgroupCheckitem -> checkgroupCheckitem.getCheckitemId())
                        .collect(Collectors.toList());
        return checkitemId;
    }
}
